<template>
	<view class="wrap bg-white">
		<view class="cu-bar bg-white">
			<u-button type="primary" class="no-border confirm-btn" @click="clear" >清空识别码</u-button>
			<view class="action" @click="toScan">
				<text class="cuIcon-scan" style="font-size: 56rpx;"></text>
			</view>
		</view>
		<view class="padding">
			<textarea  class="textarea" :focus="firstFocus" placeholder="请扫识别码!" placeholder-style="font-size:28rpx" maxlength="-1"  v-model="code" @input="search" rows="20"/>
		</view>
		<u-line color="blur"></u-line>
		<!-- <scroll-view class="scroll-list bg-white" scroll-y="true">
			<block v-for="(ietm,index) in list" :key="index">
				<u-form class="form" :model="ietm" ref="uForm" label-position="left">
					<u-form-item label="识别码:" prop="code" label-width="120" >
						<u-input v-model="ietm.code" :disabled="true" type="text" di maxlength="64"></u-input>
						<u-icon name="/static/common/img/delData.png" @click="delData(ietm.code)" color="#2979ff" class="rights" size="30"></u-icon>
					</u-form-item>
				</u-form>
			</block>
		</scroll-view> -->
		<view  style="height: 70px;" ></view>
		<view class="footer">
			<view class="confrim-btn padding padding-bottom-sm flex bg-white">
				<u-button type="success" class="no-border confirm-btn"  @click="tofrom">{{count}}</u-button>
				<u-button type="primary" class="no-border confirm-btn" @click="submit" >确认</u-button>
			</view>
			
		</view>
		<js-error mode="bottom" ref="jsError"></js-error>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				size:-1,
				count:0,
				show:false,
				code:'',
				list:[],
				firstFocus: true,
				scode:'',
			}
		},
		onLoad() {
			//获取仓库数据
			var _this = this;
			setTimeout(() => {
				_this.firstFocus = true
			},10)
		},
		methods: {
			/** 发生声音*/
			sendMp3(name){
				console.log("=====testClick=====");
				let src = '/static/jeesite/'+name+'.mp3';
				//实例化声音  
				const Audio = uni.createInnerAudioContext();
				Audio.autoplay = true;
				Audio.src = src; //音频地址  
				Audio.play(); //执行播放  
				Audio.onError((res) => {
					console.log(res.errMsg);
					console.log(res.errCode);
				});
				Audio.onPause(function() {
					console.log('end');
					Audio.destroy();
				});
			},
			tofrom(){
				var _this = this;
				uni.navigateTo({
				    url: '/pages/btdm/index/smsc/from/from',
					events: {
					   // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
					   Filter(data) {
						   _this.list = data;
						   _this.count = data.length
					   }
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						res.eventChannel.emit('initfilter', _this.list);
					}
				})
			},
			//list数据组装
			getData(code){
				var _this = this;
				if(code != '' && code.length == 24 ){
					let index = _this.list.findIndex((item) => item.code == code);
					if (index == -1) {
						_this.sendMp3("cg");
						var res = {
							code:code
						}
						_this.list.push(res)
						_this.count = Number.parseInt(_this.count) + 1
					}else{
						_this.sendMp3("sb");
					}
				}else{
					if(code != ''){
						_this.sendMp3("sb");
					}
				}
			},
			search(){
				var _this = this;
				var code = this.$u.trim(_this.code.split("\n")[_this.code.split("\n").length - 1],'all');
				this.getData(code);
			},
			clear(){
				this.firstFocus = false;
				this.code = "";
				this.scode = "";
				setTimeout(() => {
					this.firstFocus = true;
				},10)
			},
			//页面删除数据
			delData(code){
				this.firstFocus = false;
				var _this = this;
				var list = _this.list;
				for(var i = 0 ; i < list.length ; i++ ){
					if(list[i].code == code){
						list.splice(i,1);
					}
				}
				_this.count = _this.list.length
				setTimeout(() => {
					this.firstFocus = true;
				},10)
			},
			/** 扫码 */
			toScan() {
				uni.scanCode({ 
					onlyFromCamera: true,
					success: function(res) {
						this.getData(res.result);
					}.bind(this)
				});
			},
			
			submit(){
				this.firstFocus = false;
				var _this = this;
				var data = JSON.stringify(this.list);
				var parm = {
					"data":data,
				}
				this.$u.api.box.saveError(parm).then(res=>{
					if(res.result == 'true'){
						_this.code = '';
						_this.scode = "";
						_this.list = [];
						_this.$u.toast("成功!");
					}else{
						_this.$refs.jsError.showError('',res.message,'error');
						return;
					}
				})
				setTimeout(() => {
					this.firstFocus = true;
				},10)
			},
		}
	}
</script>

<style>
@import '../btdm.css';
.scroll-list {
	height: calc(100vh - var(--window-top) - var(--window-bottom) - 215px);
	width: 100%;
}
</style>
